from datetime import datetime
import json
import traceback
from django.http import JsonResponse
from django.views.generic import View
from ...models.dimensionModel import *
from ...serializers.dimensionSerializer import *
from ...utils.common_util.basic_util import get_model_serializer_config
from ...config.basic_config import type_conversion_dict
from ...config.message_config import Config_message_type, Config_message, Error_message_type
from ...utils.related_util.choice_util import get_basic_dimension_choices
from utils.responseContentUtil import *


class DimensionConfigView(View):
    def get(self, request):
        res = {"code": 400, "message": "", "data": None}
        try:
            data = {}
            query_fields = []
            query_fields = DimensionListQuerySerializer.get_query_fields()
            multi_choice_fields = ["basic_dimension_composition"]
            multi_choice_fields_dict = {}
            multi_choice_fields_dict["basic_dimension_composition"] = get_basic_dimension_choices(
            )
            for field in query_fields:
                field["type"] = type_conversion_dict[field["type"]]
                if field["dataIndex"] in multi_choice_fields:
                    field["multiChoice"] = True
                    field["choices"] = multi_choice_fields_dict[field["dataIndex"]]
            data["query_fields"] = query_fields
            table_fields = []
            table_fields = get_model_serializer_config(DimensionListSerializer)
            table_extra_fields_info = DimensionListSerializer.get_extra_fields_info()
            for extra_fields in table_extra_fields_info:
                for field in table_fields:
                    if field["dataIndex"] == extra_fields["dataIndex"]:
                        field["type"] = extra_fields["type"]
                        field["title"] = extra_fields["title"]
                        break
            table_fields.append(
                {"title": "操作", "dataIndex": "operate", "type": "operate"})
            hidden_fields = ["uuid"]
            sortable_fields = []
            link_fields = ["dimension_name"]
            for field in table_fields:
                field["type"] = type_conversion_dict[field["type"]]
                if field["dataIndex"] in hidden_fields:
                    field["hidden"] = True
                if field["dataIndex"] in sortable_fields:
                    field["sortable"] = True
                if field["dataIndex"] in link_fields:
                    field["isLink"] = True
            data["table_fields"] = table_fields
            return make_get_success_response(data=data)
        except Exception as e:
            traceback.print_exc()
            return make_server_error_response(message=str(e), data=None)


class DimensionCreateConfigView(View):
    def get(self, request):
        res = {"code": 400, "message": "", "data": None}
        try:
            data = {}
            create_fields = []
            create_fields = get_model_serializer_config(
                DimensionCreateSerializer)
            multi_choice_fields = ["basic_dimension_composition"]
            multi_choice_fields_dict = {}
            multi_choice_fields_dict["basic_dimension_composition"] = get_basic_dimension_choices(
            )
            extra_fields_info = DimensionCreateSerializer.get_extra_fields_info()
            for extra_fields in extra_fields_info:
                for field in create_fields:
                    if field["dataIndex"] == extra_fields["dataIndex"]:
                        field["type"] = extra_fields["type"]
                        field["title"] = extra_fields["title"]
                        break
            for field in create_fields:
                field["type"] = type_conversion_dict[field["type"]]
                if field["dataIndex"] in multi_choice_fields:
                    field["multiChoice"] = True
                    field["choices"] = multi_choice_fields_dict[field["dataIndex"]]

            data["create_fields"] = create_fields
            return make_get_success_response(data=data)
        except Exception as e:
            traceback.print_exc()
            return make_server_error_response(message=str(e), data=None)


class DimensionModifyInfoConfigView(View):
    def get(self, request):
        res = {"code": 400, "message": "", "data": None}
        try:
            data = {}
            modify_fields = []
            modify_fields = get_model_serializer_config(
                DimensionModifyInfoSerializer)
            multi_choice_fields = ["basic_dimension_composition"]
            multi_choice_fields_dict = {}
            multi_choice_fields_dict["basic_dimension_composition"] = get_basic_dimension_choices(
            )
            extra_fields_info = DimensionModifyInfoSerializer.get_extra_fields_info()
            for extra_fields in extra_fields_info:
                for field in modify_fields:
                    if field["dataIndex"] == extra_fields["dataIndex"]:
                        field["type"] = extra_fields["type"]
                        field["title"] = extra_fields["title"]
                        break
            for field in modify_fields:
                field["type"] = type_conversion_dict[field["type"]]
                if field["dataIndex"] in multi_choice_fields:
                    field["multiChoice"] = True
                    field["choices"] = multi_choice_fields_dict[field["dataIndex"]]
            data["modify_fields"] = modify_fields
            return make_get_success_response(data=data)
        except Exception as e:
            traceback.print_exc()
            return make_server_error_response(message=str(e), data=None)

class DimensionInfoConfigView(View):
    def get(self,request):
        res = {"code": 400, "message": "", "data": None}
        try:
            data = {}
            info_fields = []
            info_fields = get_model_serializer_config(DimensionInfoSerializer)
            extra_fields_info = DimensionInfoSerializer.get_extra_fields_info()
            for extra_fields in extra_fields_info:
                for field in info_fields:
                    if field["dataIndex"] == extra_fields["dataIndex"]:
                        field["type"] = extra_fields["type"]
                        field["title"] = extra_fields["title"]
                        break
                    
            for field in info_fields:
                field["type"] = type_conversion_dict[field["type"]]
            data["info_fields"] = info_fields
            return make_get_success_response(data=data)
        except Exception as e:
            traceback.print_exc()
            return make_server_error_response(message=str(e), data=None)
